define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Grab, $window, $timeout, $ionicSlideBoxDelegate, $ionicSideMenuDelegate, GoPage, GoDialog,$stateParams) {
		$scope.scope = new Object();
		$scope.id = $stateParams.id;
		var goods_rule = new Array();
		$scope.goods_rule = new Array();
		$scope.choose_store = new Array();
		$scope.is_all = false;
		var goods = new Array();
		var region = new Array();
		$scope.show_store = new Array();
		$scope.show_region = new Array();
		$scope.region = new Array();
		$scope.scope.choose = '';
		$scope.scope.choose_select = { region_id: 0, region_name: "请选择区域" };
		$scope.store = new Array();
		$scope.detail = new Object();
		$scope.detail.image = new Array();
		$scope.isLoad = false; //可以加载
		$scope.doSelectRegion = function () {
			var list = new Array();
			$scope.dealer.region.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.region_id;
				if (obj.name == undefined)
					obj.name = obj.region_name;
			});
			//id是值 name是显示
			GoDialog.showSelect("区域", $scope.dealer.region, $scope.scope.choose_select, function (data) {
				$scope.scope.choose = data.region_name;
				$scope.scope.choose_select = data;
			});
		}
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
			//判断关闭
			if($ionicSideMenuDelegate.isOpenRight()) {
				$ionicSideMenuDelegate.toggleRight();
			}
		};
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		//查询
		$scope.doSearch = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		//取消
		$scope.doCancel = function() {
			$ionicSideMenuDelegate.toggleRight();
		};

		$scope.onshow_post = function() {
			GoPage.go_states("newproductpost");
		}

		$scope.onshow_statistics = function() {
			GoPage.go_states("statistics");
		}
		$scope.onshow_start = function() {
			GoPage.go_states("statistics");
		}
		//图片移除
		$scope.ondelete = function(index) {
			$scope.detail.image.splice(index, 1);
		};
		//动态响应
		$scope.doShowFile = function() {
			document.getElementById("upfilebutton").click();
		}
		//图片上传
		$scope.onupload = function(val) {
			var fileList = val.files;
			for(var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				var file = fileList[i];
				reader.onload = function(e) {
					//图片进行压缩处理
					GoPicture.compress(file,e.target.result, 50, function(base64) {
						$scope.$apply(function() {
							var i=$scope.detail.image.length;
							$scope.detail.image[i] = {
								"id":'',
								"image":base64
							}
							val.value = "";
						});
					});
				}
			}
		    //上传成功
			val.value = "";
		};
		$scope.get_region = function() {
			$scope.get_store();
			if($scope.scope.choose) {
				if($scope.choose_store.length > 0) {
					for(var i = 0; i < $scope.dealer.region.length; i++) {
						if($scope.dealer.region[i].region_name == $scope.scope.choose) {
							for(var j = 0; j < $scope.region.length; j++) {
								if($scope.dealer.region[i].region_id == $scope.region[j].region_id) {
									$scope.region[i] = {
										"region_id": $scope.dealer.region[i].region_id,
										"is_all": $scope.scope.is_all,
										"store": $scope.choose_store
									}
									$scope.show_region[i] = {
										"region_name": $scope.dealer.region[i].region_name,
										"store": $scope.show_store
									}
									$scope.choose_store = new Array();
									$scope.show_store = new Array();
									return;
								}

							}
							var n = $scope.region.length;
							$scope.region[n] = {
								"region_id": $scope.dealer.region[i].region_id,
								"is_all": $scope.scope.is_all,
								"store": $scope.choose_store
							};
							$scope.show_region[n] = {
								"region_name": $scope.dealer.region[i].region_name,
								"store": $scope.show_store
							}
							$scope.choose_store = new Array();
							$scope.show_store = new Array();

						}
					}
				} else
					GoDialog.toast("请选择代理商");
			} else
				GoDialog.toast("请选择地域");
			$scope.choose_store = new Array();
			region = $scope.region;
			console.log($scope.region);

		}
		$scope.get_store = function() {
			for(var i = 0; i < $scope.store.length; i++) {
				if($scope.store[i].checked == true) {
					var n = $scope.choose_store.length;
					$scope.choose_store[n] = {
						"store_id": $scope.store[i].store_id
					};
					$scope.show_store[n] = {
						"store_name": $scope.store[i].store_name
					}
				}

			}
		}
		$scope.delete_store = function(region_name,index){
			for(var i=0;i<$scope.show_region.length;i++){
				if($scope.show_region[i].region_name==region_name){
					region[i].store.splice(index,1);
			       $scope.show_region[i].store.splice(index,1);
				}
			}
			
		}
		$scope.delete_region = function(index){
			$scope.show_region.splice(index,1);
			region.splice(index,1);
		}
		$scope.select_all = function(all) {
			if(all == true) {
				$scope.scope.is_all = true;
				for(var i = 0; i < $scope.store.length; i++) {
					$scope.store[i].checked = true;
				}
			} else {
				$scope.scope.is_all = false;
				for(var i = 0; i < $scope.store.length; i++) {
					$scope.store[i].checked = false;
				}
			}

			console.log($scope.store);
		}
		$scope.checked = function(id) {
			console.log(id);
			for(var i = 0; i < $scope.store.length; i++) {
				if($scope.store[i].store_id == id) {
					if($scope.store[i].checked == false) {
						$scope.scope.is_all = false;
						
						break;
					} else {
						if($scope.store.length == 1) {
							$scope.scope.is_all = true;
							
						}

					}
				} else if($scope.store[i].checked == true) {
					$scope.scope.is_all = true;
					

				} else {
					
					$scope.scope.is_all = false;
					break;
				}
			}
			//			console.log()
			console.log($scope.dealer);
		}
		$scope.get_goods = function() {
			goods = new Array();
			console.log($scope.dealer);
			for(var i = 0; i < $scope.dealer.goods.length; i++) {
				if($scope.dealer.goods[i].promotion_price!=undefined|$scope.dealer.goods[i].promotion_price!=''){
					var n = goods.length;
					goods[n] = {
						"goods_id": $scope.dealer.goods[i].goods_id,
						"promotion_price": $scope.dealer.goods[i].preprice
					};
				}
			}
			if(goods.length<1){
				GoDialog.toast("活动商品信息未完整");
			}
		}

		$scope.get_third = function() {
			//看情况取消注释
			$scope.get_region();
			

		}
		$scope.submit = function() {
			$scope.get_goods();
			if(region.length < 1) {
				GoDialog.toast("请确定您已经保存修改的促销区域信息");
				return;
			}
			for(var i=0;i<region.length;i++){
				if(region[i].store==undefined){
					GoDialog.toast("请填写完整的代理商信息");
				return;
				}else if(region[i].store.length<1){
					GoDialog.toast("请填写完整的代理商信息");
				return;
				}
			}
		
				Grab.grab_joinedit($scope.id,region,  goods, function(data) {
					if(data.success == true) {
						GoDialog.toast("修改成功");
					} else
						GoDialog.toast("修改失败");
				})
			}
		

		$scope.init_data = function() {
			Grab.grab_detail($scope.id, function(data) {
				$scope.detail = data;

			});
//			Grab.goods_list($scope.id,function(data){
//				$scope.goods = data;
//				console.log(data);
//			});
			Grab.grab_dealer($scope.id, function(data) {
				$scope.dealer = data;
				$scope.isable = $scope.dealer.change_price;
				$scope.$watch('scope.choose', function(newValue, oldValue) {
					for(var i = 0; i < $scope.dealer.region.length; i++) {
						if($scope.dealer.region[i].region_name == newValue){
							$scope.store = $scope.dealer.region[i].store;
							$scope.select_all($scope.dealer.region[i].is_all);
							
						}
					}
				})
			});
		}
		$scope.init_data();

	}

	controller.$inject = ['$scope', 'Grab', '$window', '$timeout', '$ionicSlideBoxDelegate', '$ionicSideMenuDelegate', 'GoPage', 'GoDialog','$stateParams'];

	return app.registerController("grabctrl",controller);
});